package com.qigao.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.AccountInfoService;
import com.qigao.mall.commons.dto.AccountInfoQueryDto;
import com.qigao.mall.commons.dto.AccountInfoResultDto;
import com.qigao.mall.commons.dto.SettleQueryDto;
import com.qigao.mall.commons.dto.SettleTotalResultDto;
import com.qigao.mall.commons.enums.*;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.commons.utils.IDUtils;
import com.qigao.mall.jdbc.mapper.SysUserMapper;
import com.qigao.mall.jdbc.mapper.TbAccountInfoMapper;
import com.qigao.mall.jdbc.mapper.TbMerInfoMapper;
import com.qigao.mall.jdbc.model.SysUser;
import com.qigao.mall.jdbc.model.TbAccountInfo;
import com.qigao.mall.jdbc.model.TbMerInfo;
import com.qigao.mall.jdbc.model.TbSettleTotal;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName three-mall
 * @date 2020-04-2020/4/10 14:33
 * @Description:
 */
@Service
public class AccountInfoServiceImpl implements AccountInfoService {
    @Resource
    private TbAccountInfoMapper tbAccountInfoMapper;
    @Resource
    private TbMerInfoMapper tbMerInfoMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public TbAccountInfo saveMerAccount(String merNo,AccountTypeEnum accountTypeEnum) {
        if(StringUtils.isEmpty(merNo))return null;
        TbAccountInfo tbAccountInfo=tbAccountInfoMapper.selectByOwnerNoAndAccountType(merNo, accountTypeEnum.getCode());
        if(tbAccountInfo==null){
            tbAccountInfo=new TbAccountInfo();
            TbMerInfo tbMerInfo=tbMerInfoMapper.selectByMerNo(merNo);
            tbAccountInfo.setOwnerType(AccountOwnerTypeEnum.MER.getCode());
            tbAccountInfo.setOwnerNo(merNo);
            tbAccountInfo.setOwnerName(tbMerInfo.getMerName());
            String accountNo= IDUtils.genIdStr(IdTypeEnum.ACCOUNT);
            tbAccountInfo.setAccountNo(accountNo);
            tbAccountInfo.setAccountName(tbMerInfo.getMerName()+"("+accountTypeEnum.getName()+")");
            tbAccountInfo.setAccountType(accountTypeEnum.getCode());
            tbAccountInfo.setAccountAmt(BigDecimal.ZERO);
            tbAccountInfo.setCreateTime(new Date());
            tbAccountInfo.setModiTime(new Date());
            tbAccountInfoMapper.insertSelective(tbAccountInfo);
        }
        return tbAccountInfo;
    }


    @Override
    public Result<PageInfo<AccountInfoResultDto>> listPage(AccountInfoQueryDto accountInfoQueryDto) {
        PageHelper.startPage(accountInfoQueryDto.getPageNum(),accountInfoQueryDto.getPageSize());
        Page<TbAccountInfo> accountInfoPage=(Page<TbAccountInfo>)tbAccountInfoMapper.selectByCond(accountInfoQueryDto);
        return convertToDto(accountInfoPage);
    }

    @Override
    public List<AccountInfoResultDto> findOrderResultByCond(AccountInfoQueryDto accountInfoQueryDto) {
        List<TbAccountInfo> accountInfoList=tbAccountInfoMapper.selectByCond(accountInfoQueryDto);
        List<AccountInfoResultDto> accountInfoResultDtos=new ArrayList<>();
        for(TbAccountInfo tbAccountInfo:accountInfoList){
            AccountInfoResultDto accountInfoResultDto=new AccountInfoResultDto();
            BeanUtils.copyProperties(tbAccountInfo,accountInfoResultDto);
            accountInfoResultDto.setAccountTypeName(AccountTypeEnum.getNameByCode(tbAccountInfo.getAccountType()));
            accountInfoResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountInfo.getCreateTime()));
            accountInfoResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountInfo.getModiTime()));
            accountInfoResultDtos.add(accountInfoResultDto);
        }
        return accountInfoResultDtos;
    }

    @Override
    public Result<AccountInfoResultDto> findAccountByUserNo(String userNo) {
        TbAccountInfo tbAccountInfo=tbAccountInfoMapper.selectByOwnerNoAndAccountType(userNo, AccountTypeEnum.CASH_ACCOUNT.getCode());
        if(tbAccountInfo==null){
            tbAccountInfo=saveUserAccount(userNo,AccountTypeEnum.CASH_ACCOUNT);
        }
        AccountInfoResultDto accountInfoResultDto=new AccountInfoResultDto();
        BeanUtils.copyProperties(tbAccountInfo,accountInfoResultDto);
        accountInfoResultDto.setAccountTypeName(AccountTypeEnum.getNameByCode(tbAccountInfo.getAccountType()));
        accountInfoResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountInfo.getCreateTime()));
        accountInfoResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountInfo.getModiTime()));
        return Result.newSuccess(accountInfoResultDto);
    }

    @Override
    public Result<AccountInfoResultDto> findScoreAccount(String userNo) {
        TbAccountInfo tbAccountInfo=tbAccountInfoMapper.selectByOwnerNoAndAccountType(userNo, AccountTypeEnum.SCORE_ACCOUNT.getCode());
        if(tbAccountInfo==null){
            tbAccountInfo=saveUserAccount(userNo,AccountTypeEnum.SCORE_ACCOUNT);
        }
        AccountInfoResultDto accountInfoResultDto=new AccountInfoResultDto();
        BeanUtils.copyProperties(tbAccountInfo,accountInfoResultDto);
        accountInfoResultDto.setAccountTypeName(AccountTypeEnum.getNameByCode(tbAccountInfo.getAccountType()));
        accountInfoResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountInfo.getCreateTime()));
        accountInfoResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountInfo.getModiTime()));
        return Result.newSuccess(accountInfoResultDto);
    }

    @Override
    public Result<AccountInfoResultDto> findAccountByType(String userNo, String accountType) {
        TbAccountInfo tbAccountInfo=tbAccountInfoMapper.selectByOwnerNoAndAccountType(userNo, accountType);
        if(tbAccountInfo==null){
            tbAccountInfo=saveUserAccount(userNo,AccountTypeEnum.parse(accountType));
        }
        AccountInfoResultDto accountInfoResultDto=new AccountInfoResultDto();
        BeanUtils.copyProperties(tbAccountInfo,accountInfoResultDto);
        accountInfoResultDto.setAccountTypeName(AccountTypeEnum.getNameByCode(tbAccountInfo.getAccountType()));
        accountInfoResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountInfo.getCreateTime()));
        accountInfoResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountInfo.getModiTime()));
        return Result.newSuccess(accountInfoResultDto);
    }

    private Result<PageInfo<AccountInfoResultDto>> convertToDto(Page<TbAccountInfo> accountInfoPage) {
        Page<AccountInfoResultDto> accountInfoResultDtoPage=new Page<AccountInfoResultDto>();
        BeanUtils.copyProperties(accountInfoPage,accountInfoResultDtoPage);
        if(accountInfoPage.getResult()!=null){
            for(TbAccountInfo tbAccountInfo:accountInfoPage.getResult()){
                AccountInfoResultDto accountInfoResultDto=new AccountInfoResultDto();
                BeanUtils.copyProperties(tbAccountInfo,accountInfoResultDto);
                accountInfoResultDto.setAccountTypeName(AccountTypeEnum.getNameByCode(tbAccountInfo.getAccountType()));
                accountInfoResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountInfo.getCreateTime()));
                accountInfoResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountInfo.getModiTime()));
                accountInfoResultDtoPage.add(accountInfoResultDto);
            }
        }
        return Result.newSuccess(new PageInfo<>(accountInfoResultDtoPage));
    }

    @Override
    public TbAccountInfo saveUserAccount(String userId, AccountTypeEnum accountTypeEnum) {
        if(StringUtils.isEmpty(userId))return null;
        TbAccountInfo tbAccountInfo=tbAccountInfoMapper.selectByOwnerNoAndAccountType(userId, accountTypeEnum.getCode());
        if(tbAccountInfo==null){
            tbAccountInfo=new TbAccountInfo();
            SysUser sysUser=sysUserMapper.selectByUserNo(userId);
            tbAccountInfo.setOwnerType(AccountOwnerTypeEnum.USER.getCode());
            tbAccountInfo.setOwnerNo(userId);
            tbAccountInfo.setOwnerName(sysUser.getNickName());
            String accountNo= IDUtils.genIdStr(IdTypeEnum.ACCOUNT);
            tbAccountInfo.setAccountNo(accountNo);
            tbAccountInfo.setAccountName(sysUser.getNickName()+"("+accountTypeEnum.getName()+")");
            tbAccountInfo.setAccountType(accountTypeEnum.getCode());
            tbAccountInfo.setAccountAmt(BigDecimal.ZERO);
            tbAccountInfo.setCompNo(sysUser.getCompNo());
            tbAccountInfo.setCompName(sysUser.getCompName());
            tbAccountInfo.setCreateTime(new Date());
            tbAccountInfo.setModiTime(new Date());
            tbAccountInfoMapper.insertSelective(tbAccountInfo);
        }
        return tbAccountInfo;
    }

}
